using System;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class GGStoryAchivementsController : ScriptableObject
{
	public delegate void OnAchievementAccomplished(GameplayAchievement achievement);

	public class UpdateAchievementParams
	{
	}

	public class UpdateAchievementOnMatchResultParams : UpdateAchievementParams
	{
		public MatchParameters.MatchParams matchParams;

		public bool isPlayerWinner;

		public int scoreDifference;

		public List<SuccessInARowAchievement.WinType> winTypes;
	}

	public class UpdateAchievementResourceCollected : UpdateAchievementParams
	{
		public MatchParameters.MatchParams matchParams;

		public int scoreEarned;

		public CollectorAchievement.CollectionType collectionType;
	}

	private const string storyAchievementFilename = "storyachivements.bytes";

	private AchivementsDAO storyModel;

	private const string gameplayAchievementFilename = "gameplayachievements.bytes";

	private GameplayAchievementsDAO gameplayModel;

	public List<SkillAchievement> skillAchievements = new List<SkillAchievement>();

	public List<MatchAchievement> matchAchievements = new List<MatchAchievement>();

	public List<OwnedItemsAchievement> itemAchievements = new List<OwnedItemsAchievement>();

	public List<CollectorAchievement> collectorAchievements = new List<CollectorAchievement>();

	public List<SuccessInARowAchievement> successInARowAchievements = new List<SuccessInARowAchievement>();

	public List<LevelAchievement> levelAchievements = new List<LevelAchievement>();

	public List<LevelNameAchievement> levelNameAchievements = new List<LevelNameAchievement>();

	public List<FacebookFriendsAchievement> facebookFriendAchievements = new List<FacebookFriendsAchievement>();

	private Dictionary<AchievementResource.AchievementType, List<GameplayAchievement>> m_gameplayAchievements;

	public List<AchievementResource> achievementResources = new List<AchievementResource>();

	[NonSerialized]
	public Dictionary<string, GameplayAchievement> achievementsMap = new Dictionary<string, GameplayAchievement>();

	[HideInInspector]
	public List<ReferencableObject> achievements = new List<ReferencableObject>();

	public static GGStoryAchivementsController instance_;

	private Dictionary<AchievementResource.AchievementType, List<GameplayAchievement>> gameplayAchievements
	{
		get
		{
			if (m_gameplayAchievements == null)
			{
				m_gameplayAchievements = CreateGameplayAchievements();
			}
			return m_gameplayAchievements;
		}
	}

	public static GGStoryAchivementsController instance
	{
		get
		{
			if (instance_ == null)
			{
				instance_ = Resources.Load("StoryAchivements", typeof(GGStoryAchivementsController)) as GGStoryAchivementsController;
				instance_.Init();
			}
			return instance_;
		}
	}

	public GGStoryAchivementBase activeStoryAchivement
	{
		get
		{
			return null;
		}
	}

	public event OnAchievementAccomplished onAchievementAccomplished;

	public void MarkDirty()
	{
		m_gameplayAchievements = null;
		achievementsMap.Clear();
		foreach (List<GameplayAchievement> value in gameplayAchievements.Values)
		{
			foreach (GameplayAchievement item in value)
			{
				if (!item.hasValidId || achievementsMap.ContainsKey(item.id))
				{
					item.CreateId();
					achievements.Add(item.GetReference());
				}
				achievementsMap.Add(item.id, item);
			}
		}
		List<ReferencableObject> list = new List<ReferencableObject>();
		foreach (ReferencableObject achievement in achievements)
		{
			if (!achievementsMap.ContainsKey(achievement.id))
			{
				list.Add(achievement);
			}
		}
		foreach (ReferencableObject item2 in list)
		{
			achievements.Remove(item2);
		}
		foreach (ReferencableObject achievement2 in achievements)
		{
			achievement2.referenceName = achievementsMap[achievement2.id].name;
		}
	}

	private void OnEnable()
	{
		base.hideFlags = HideFlags.None;
	}

	protected Dictionary<AchievementResource.AchievementType, List<GameplayAchievement>> CreateGameplayAchievements()
	{
		Dictionary<AchievementResource.AchievementType, List<GameplayAchievement>> dictionary = new Dictionary<AchievementResource.AchievementType, List<GameplayAchievement>>();
		dictionary.Clear();
		List<GameplayAchievement> list = new List<GameplayAchievement>();
		foreach (SkillAchievement skillAchievement in skillAchievements)
		{
			list.Add(skillAchievement);
		}
		dictionary.Add(AchievementResource.AchievementType.SkillAchievement, list);
		List<GameplayAchievement> list2 = new List<GameplayAchievement>();
		foreach (MatchAchievement matchAchievement in matchAchievements)
		{
			list2.Add(matchAchievement);
		}
		dictionary.Add(AchievementResource.AchievementType.MatchAchievement, list2);
		List<GameplayAchievement> list3 = new List<GameplayAchievement>();
		foreach (OwnedItemsAchievement itemAchievement in itemAchievements)
		{
			list3.Add(itemAchievement);
		}
		dictionary.Add(AchievementResource.AchievementType.ItemAchievement, list3);
		List<GameplayAchievement> list4 = new List<GameplayAchievement>();
		foreach (CollectorAchievement collectorAchievement in collectorAchievements)
		{
			list4.Add(collectorAchievement);
		}
		dictionary.Add(AchievementResource.AchievementType.CollectorAchievement, list4);
		List<GameplayAchievement> list5 = new List<GameplayAchievement>();
		foreach (SuccessInARowAchievement successInARowAchievement in successInARowAchievements)
		{
			list5.Add(successInARowAchievement);
		}
		dictionary.Add(AchievementResource.AchievementType.SuccessInARowAchievement, list5);
		List<GameplayAchievement> list6 = new List<GameplayAchievement>();
		foreach (LevelAchievement levelAchievement in levelAchievements)
		{
			list6.Add(levelAchievement);
		}
		dictionary.Add(AchievementResource.AchievementType.LevelAchievement, list6);
		List<GameplayAchievement> list7 = new List<GameplayAchievement>();
		foreach (LevelNameAchievement levelNameAchievement in levelNameAchievements)
		{
			list7.Add(levelNameAchievement);
		}
		dictionary.Add(AchievementResource.AchievementType.LevelNameAchievement, list7);
		List<GameplayAchievement> list8 = new List<GameplayAchievement>();
		foreach (FacebookFriendsAchievement facebookFriendAchievement in facebookFriendAchievements)
		{
			list8.Add(facebookFriendAchievement);
		}
		dictionary.Add(AchievementResource.AchievementType.FacebookFriendsAchievement, list8);
		return dictionary;
	}

	protected virtual void Init()
	{
		this.onAchievementAccomplished = null;
		MarkDirty();
		ReloadModel();
	}

	public List<GameplayAchievement> GetAchievementsForType(AchievementResource.AchievementType achievementType)
	{
		List<GameplayAchievement> value = null;
		gameplayAchievements.TryGetValue(achievementType, out value);
		return value;
	}

	public void ReloadModel()
	{
		if (!ProtoIO.LoadFromFileCloudSync<AchivementsDAO>("storyachivements.bytes", out storyModel))
		{
			storyModel = new AchivementsDAO();
			storyModel.achivements = new List<AchivementDAO>();
			ProtoIO.SaveToFile("storyachivements.bytes", storyModel);
		}
		if (storyModel.achivements == null)
		{
			storyModel.achivements = new List<AchivementDAO>();
		}
		if (!ProtoIO.LoadFromFileCloudSync<GameplayAchievementsDAO>("gameplayachievements.bytes", out gameplayModel))
		{
			gameplayModel = new GameplayAchievementsDAO();
			gameplayModel.achievements = new List<GameplayAchievementDAO>();
			ProtoIO.SaveToFile("gameplayachievements.bytes", gameplayModel);
		}
		if (gameplayModel.achievements == null)
		{
			gameplayModel.achievements = new List<GameplayAchievementDAO>();
		}
		for (int i = 0; i < achievements.Count; i++)
		{
			GameplayAchievement value = null;
			achievementsMap.TryGetValue(achievements[i].id, out value);
			GameplayAchievementDAO orCreateGameplayAchievementForName = GetOrCreateGameplayAchievementForName(gameplayModel, value.name);
			value.Init(this, orCreateGameplayAchievementForName, i);
		}
	}

	private GameplayAchievementDAO GetOrCreateGameplayAchievementForName(GameplayAchievementsDAO achievements, string name)
	{
		foreach (GameplayAchievementDAO achievement in achievements.achievements)
		{
			if (achievement.name == name)
			{
				if (achievement.items == null)
				{
					achievement.items = new List<string>();
				}
				return achievement;
			}
		}
		GameplayAchievementDAO gameplayAchievementDAO = new GameplayAchievementDAO();
		gameplayAchievementDAO.name = name;
		gameplayAchievementDAO.items = new List<string>();
		achievements.achievements.Add(gameplayAchievementDAO);
		return gameplayAchievementDAO;
	}

	public void Save()
	{
		if (storyModel == null)
		{
			storyModel = new AchivementsDAO();
			storyModel.achivements = new List<AchivementDAO>();
		}
		ProtoIO.SaveToFileCloudSync("storyachivements.bytes", storyModel);
		if (gameplayModel == null)
		{
			gameplayModel = new GameplayAchievementsDAO();
			gameplayModel.achievements = new List<GameplayAchievementDAO>();
		}
		ProtoIO.SaveToFileCloudSync("gameplayachievements.bytes", gameplayModel);
	}

	public void UpdateAchievements(UpdateAchievementParams updateParams)
	{
		foreach (KeyValuePair<string, GameplayAchievement> item in achievementsMap)
		{
			item.Value.UpdateAchievement(updateParams);
		}
	}

	public void AchievementAccomplishedNotify(GameplayAchievement achievement)
	{
		if (this.onAchievementAccomplished != null)
		{
			this.onAchievementAccomplished(achievement);
		}
	}
}
